/**
 * amn - jQuery calendar
 * enkapsulasi pembuatan calendar, sehingga kompatibel dengan
 * generate-an calendar menggunakan calendar.js 
 */
Calendar = function () {
	
};

Calendar.setup = function(params) {
    var isBirthdate = false;
    var txt_calendar = jQuery('#' + params['inputField']);
    var parent_form = jQuery('#' + params['inputField']).closest('form');
    if (params['isBirthdate'] != undefined && params['isBirthdate'] != null) {
        isBirthdate = params['isBirthdate'];
    }
    txt_calendar.attr("autocomplete", "off");
    // get original field name
    var original_name = txt_calendar.prop('name');
    txt_calendar.prop('name', '_' + original_name);
    txt_calendar.prop('class', 'form-control cal hasDatePicker');

    // datepicker button trigger
    if (params['button'] != undefined && params['button'] != null) {
        jQuery('#' + params['button']).click(function() {
            jQuery("#" + params['inputField']).datepicker("show");
        });
    }

    // create new hidden field
    jQuery('<input>').attr({
        type: 'hidden',
        id: 'txt_' + params['inputField'],
        name: original_name
    }).appendTo(parent_form);

    // copy value from existing calendar to hidden field
    var hidden_calendar = jQuery('#txt_' + params['inputField']);
    hidden_calendar.val(txt_calendar.val());

    // format calendar text
    var d = jQuery.datepicker.parseDate('dd/mm/yy', txt_calendar.val());

    txt_calendar.val(jQuery.datepicker.formatDate('dd.mm.yy', d));

    // create calendar
    if (!isBirthdate) {
        txt_calendar.datepicker({
            dateFormat: 'dd.mm.yy',
            changeMonth: true,
            changeYear: true,
            yearRange: "c-5:c+5",
            //showButtonPanel: true,
            altField: '#txt_' + params['inputField'],
            altFormat: 'dd/mm/yy',
            pickTime: false,
            onClose: function(tgl) {
                if (tgl == '') {
                    hidden_calendar.val('');
                } else {
                    if (tgl.indexOf('.') == -1) {
                        try {
                            var dd = tgl.charAt(0) + tgl.charAt(1);
                            var mm = tgl.charAt(2) + tgl.charAt(3);
                            var yy = tgl.charAt(4) + tgl.charAt(5) + tgl.charAt(6) + tgl.charAt(7);

                            var t = jQuery.datepicker.parseDate('dd.mm.yy', dd + '.' + mm + '.' + yy);
                            txt_calendar.val(jQuery.datepicker.formatDate('dd.mm.yy', t));
                            hidden_calendar.val(jQuery.datepicker.formatDate('dd/mm/yy', t));
                        } catch (err) {
                            txt_calendar.val('');
                            hidden_calendar.val('');
                        }
                    }
                }
            }
        });
    } else {
        var date = new Date();
        var year = date.getFullYear() - 38;
        txt_calendar.datepicker({
            dateFormat: 'dd.mm.yy',
            changeMonth: true,
            changeYear: true,
            yearRange: "-100:-14",
            //showButtonPanel: true,
            altField: '#txt_' + params['inputField'],
            altFormat: 'dd/mm/yy',
            defaultDate: new Date(year, 00, 01),
            pickTime: false,
            onClose: function(tgl) {
                if (tgl == '') {
                    hidden_calendar.val('');
                } else {
                    if (tgl.indexOf('.') == -1) {
                        try {
                            var dd = tgl.charAt(0) + tgl.charAt(1);
                            var mm = tgl.charAt(2) + tgl.charAt(3);
                            var yy = tgl.charAt(4) + tgl.charAt(5) + tgl.charAt(6) + tgl.charAt(7);

                            var t = jQuery.datepicker.parseDate('dd.mm.yy', dd + '.' + mm + '.' + yy);
                            txt_calendar.val(jQuery.datepicker.formatDate('dd.mm.yy', t));
                            hidden_calendar.val(jQuery.datepicker.formatDate('dd/mm/yy', t));
                        } catch (err) {
                            txt_calendar.val('');
                            hidden_calendar.val('');
                        }
                    }
                }
            }
        });
    }
}